<template>
  <div>
    <div class="app-container">
      <el-form
        :model="queryParams"
        ref="queryRef"
        v-show="showSearch"
        :inline="true"
      >
      <el-form-item label="用户名称" prop="userName">
        <el-select
          v-model="queryParams.userName"
          filterable
          remote
          reserve-keyword
          placeholder="请选择用户名称"
          :remote-method="getUserOption"
          :loading="userLoading"
        >
          <el-option
            v-for="item in userOptions"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="Search" @click="handleQuery"
            >搜索</el-button
          >
          <el-button icon="Refresh" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>
      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button type="primary" plain icon="Refresh" @click="handleRefresh"
            >刷新缓存</el-button
          >
        </el-col>
        <right-toolbar
          v-model:showSearch="showSearch"
          @queryTable="getList"
        ></right-toolbar>
      </el-row>
      <el-table v-loading="loading" height="70vh" :data="dataList">
        <el-table-column label="序号" type="index" width="50" />
        <el-table-column label="编号" prop="id" align="center" />
        <el-table-column label="用户名" prop="redisKey" align="center" />
        <el-table-column label="截至编码" prop="redisValue" align="center" />
        <el-table-column
          label="操作"
          align="center"
          width="80"
          fixed="right"
          class-name="small-padding fixed-width"
        >
          <template #default="scope">
            <el-tooltip content="修改" placement="top">
              <el-button
                link
                type="primary"
                icon="Edit"
                @click="handleUpdate(scope.row.id)"
                v-hasPermi="['system:recycleCode:edit']"
              ></el-button>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>
      <pagination
        v-show="total > 0"
        :total="total"
        v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize"
        @pagination="getList"
      />
    </div>
    <edit
      v-model:visible="addParams.visible"
      :params="addParams"
      @refresh="getList"
    />
  </div>
</template>
<script setup name="RecycleCode">
import edit from "./edit.vue";
import { listTable, refreshCache } from "@/api/system/recycleCode";
import { userOptionSelect } from "@/api/system/user";
const { proxy } = getCurrentInstance();
const dataList = ref([]);
const total = ref(0);
const loading = ref(true);
const showSearch = ref(true);
const addParams = ref({
  visible: false,
  id: undefined,
});

const data = reactive({
  queryParams: {
    pageNum: 1,
    pageSize: 20,
    userName: undefined,
  },
});
const { queryParams } = toRefs(data);

/** 用户下拉框 */
const userLoading = ref(false);
const userOptions = ref([]);
async function getUserOption(query) {
  if (query) {
    userLoading.value = true;
    const { data } = await userOptionSelect({ queryKey: query });
    userOptions.value = data.map((i) => {
      const { userId, nickName, userName } = i;
      return {
        value: userName,
        label: `${userId},${nickName},${userName}`,
      };
    });
    userLoading.value = false;
  }
}
getUserOption(" ");
/** 查询列表 */
function getList() {
  loading.value = true;
  listTable(queryParams.value).then((response) => {
    dataList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}
/** 刷新redis缓存 */
const handleRefresh = async () => {
  await refreshCache();
  proxy.$modal.msgSuccess("刷新redis缓存成功");
};
/** 搜索按钮操作 */
function handleQuery() {
  queryParams.pageNum = 1;
  getList();
}
/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}
/** 修改商品*/
function handleUpdate(id) {
  addParams.value = {
    id,
    visible: true,
  };
}
getList();
</script>
